package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.converter.GoodsConverter;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 顾海龙
 * @version 1.0
 */
@RestController
@ResponseBody
public class GoodsController {

    @Autowired
    GoodsService goodsService;


    //添加商品
    @PostMapping("/admin/goods")
    @SaAdminCheckPermission(SystemPermissions.Goods.ADD)
    public Result addGoods(@RequestBody GoodsCommand goodsCommand){
        try {
            goodsService.addGoods(goodsCommand);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //修改商品
    @PutMapping("/admin/goods")
    @SaAdminCheckPermission(SystemPermissions.Goods.UPDATE)
    public Result updateGoods(@RequestBody GoodsCommand goodsCommand){
        try {
            goodsService.updateGoods(goodsCommand);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }


    //修改商品状态
    @PutMapping("/admin/goods/status/{goodsId}")
    @SaAdminCheckPermission(SystemPermissions.Goods.UPDATE)
    public Result updateGoodsStatus(@PathVariable Long goodsId,@RequestParam Integer status){
        try {
            goodsService.updateGoodsStatus(goodsId,status);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //获取商品分页列表
    @GetMapping("/admin/goods/list")
    @SaAdminCheckPermission(SystemPermissions.Goods.SEARCH)
    public Result selectGoodsList(@RequestParam Integer pageNum,@RequestParam Integer count,@RequestParam String keyword){
        PageDTO<GoodsDTO> goodsPage = null;
        try {
            PageRequest pageRequest = new PageRequest();
            pageRequest.setPageNum(pageNum);
            pageRequest.setKeyword(keyword);
            pageRequest.setCount(count);
            goodsPage = goodsService.getGoodsPage(pageRequest);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(goodsPage);
    }

    //删除商品
    @DeleteMapping("/admin/goods/{goodsId}")
    @SaAdminCheckPermission(SystemPermissions.Goods.DELETE)
    public Result deleteGoods(@PathVariable Long goodsId){
        DeleteGoodsOperateDTO deleteGoodsOperateDTO = null;
        try {
            deleteGoodsOperateDTO = goodsService.deleteGoods(goodsId);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(deleteGoodsOperateDTO);
    }

    //删除选择商品（批量删除）
    @DeleteMapping("/admin/goods/selected")
    @SaAdminCheckPermission(SystemPermissions.Goods.BATCH_DELETE)
    public Result deleteGoodsList(@RequestBody BatchDeleteCommand batchDeleteCommand){
        List<DeleteGoodsOperateDTO> deleteGoodsOperateDTOS = null;
        try {
            deleteGoodsOperateDTOS = goodsService.deleteGoodsSelected(batchDeleteCommand);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(deleteGoodsOperateDTOS);
    }
}
